package com.easychat.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.entity.dto.TokenUserInfoDto;
import com.easychat.entity.enums.GroupStatusEnum;
import com.easychat.entity.enums.MessageTypeEnum;
import com.easychat.entity.enums.UserContactStatusEnum;
import com.easychat.entity.po.UserContact;
import com.easychat.entity.query.GroupInfoQuery;
import com.easychat.entity.po.GroupInfo;
import com.easychat.entity.query.UserContactQuery;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.entity.vo.groupinfo.request.SaveGroupReqVO;
import com.easychat.entity.vo.groupinfo.response.GetGroupInfo4ChatRspVO;
import com.easychat.exception.BusinessException;
import com.easychat.service.GroupInfoService;
import com.easychat.service.UserContactService;
import com.easychat.service.impl.UserContactServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 *  Controller
 */
@RestController("groupInfoController")
@RequestMapping("/group")
public class GroupInfoController extends ABaseController{

	@Resource
	private GroupInfoService groupInfoService;

	@Resource
	private UserContactService contactService;
    @Autowired
    private UserContactServiceImpl userContactService;

	@GlobalInterceptor
	@RequestMapping("/saveGroup")
	public ResponseVO saveGroup(HttpServletRequest request, SaveGroupReqVO vo) throws IOException {
		TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);

		GroupInfo groupInfo = GroupInfo.builder()
				.groupOwnerId(tokenUserInfoDto.getUserId())
				.groupId(vo.getGroupId())
				.groupNotice(vo.getGroupNotice())
				.groupName(vo.getGroupName())
				.joinType(vo.getJoinType())
				.build();

		groupInfoService.saveGroup(groupInfo, vo.getAvatarFile(), vo.getAvatarCover());
		return getSuccessResponseVO(null);
	}

	@GlobalInterceptor
	@RequestMapping("/loadMyGroup")
	public ResponseVO loadMyGroupList(HttpServletRequest request) {
		TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
		List<GroupInfo> groupInfoList = groupInfoService.selectMyGroupList(tokenUserInfoDto.getUserId());
		return getSuccessResponseVO(groupInfoList);
	}

	@GlobalInterceptor
	@RequestMapping("/getGroupInfo")
	public ResponseVO getGroupInfo(HttpServletRequest request, @NotEmpty String groupId) {
		GroupInfo groupInfo = getGroupInfoCommon(request, groupId);
		UserContactQuery userContactQuery = new UserContactQuery();
		userContactQuery.setContactId(groupId);
		Integer menberCount = userContactService.findCountByParam(userContactQuery);
		groupInfo.setMemberCount(menberCount);

		return getSuccessResponseVO(groupInfo);
	}

	@GlobalInterceptor
	@RequestMapping("/getGroupInfo4Chat")
	public ResponseVO getGroupInfo4Chat(HttpServletRequest request, @NotEmpty String groupId) {
		GroupInfo groupInfo = getGroupInfoCommon(request, groupId);

		List<UserContact> userContactList = userContactService.selectUserContactListByGroupId(groupId);

		GetGroupInfo4ChatRspVO vo = GetGroupInfo4ChatRspVO.builder()
				.groupInfo(groupInfo)
				.userContactList(userContactList)
				.build();

		return getSuccessResponseVO(vo);
	}

	@GlobalInterceptor
	@RequestMapping("/addOrRemoveGroupUser")
	public ResponseVO addOrRemoveGroupUser(HttpServletRequest request,
										   @NotEmpty String groupId,
										   @NotEmpty String selectContacts,
										   @NotNull Integer opType ) {

		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);

		this.groupInfoService.addOrRemoveGroupUser(tokenUserInfo, groupId, selectContacts, opType);

		return getSuccessResponseVO(null);
	}

	@GlobalInterceptor
	@RequestMapping("/leaveGroup")
	public ResponseVO leaveGroup(HttpServletRequest request,
										   @NotEmpty String groupId) {

		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);

		this.groupInfoService.leaveGroup(tokenUserInfo.getUserId(), groupId, MessageTypeEnum.LEAVE_GROUP);

		return getSuccessResponseVO(null);
	}

	@GlobalInterceptor
	@RequestMapping("/dissolutionGroup")
	public ResponseVO dissolutionGroup(HttpServletRequest request,
								 @NotEmpty String groupId) {

		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
		this.groupInfoService.dissolutionGroup(tokenUserInfo.getUserId(), groupId);
		return getSuccessResponseVO(null);
	}

	private GroupInfo getGroupInfoCommon(HttpServletRequest request, String groupId) {
		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
		UserContact userContact = this.contactService.getUserContactByUserIdAndContactId(tokenUserInfo.getUserId(), groupId);
		if (null == userContact || !userContact.getStatus().equals(UserContactStatusEnum.FRIEND.getStatus())) {
			throw new BusinessException("您还不是群成员，不能查看群信息");
		}
		GroupInfo dbGroupInfo = groupInfoService.getGroupInfoByGroupId(groupId);
		if (null == dbGroupInfo || !dbGroupInfo.getStatus().equals(GroupStatusEnum.NORMAL.getStatus())) {
			throw new BusinessException("群不存在或已解散");
		}
		return dbGroupInfo;
	}

}